import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
  namespace: 'houseHome',
  state: {
    listData: [], // 列表数据
    showDrawer:false,
    showModel:false,
    houseTypeRange: [{
      type: 'workshop',
      text: '厂房'
    }, {
      type: 'warehouse',
      text: '仓库'
    }, {
      type: 'land',
      text: '空地'
    }],
    houseType: {
      type: 'workshop',
      text: '厂房'
    },
    floorTypeRange: [{
      text: '不限',
      value: 0
    }, {
      text: '1楼',
      value: 1
    }, {
      text: '2楼及以上',
      value: 2
    }],
    floorType: {
      text: '不限',
      value: 0
    },
    areaTypeRange: [{
      minArea: 0,
      maxArea: 0,
      text: '不限'
    }, {
      minArea: 0,
      maxArea: 500,
      text: '0~500m²'
    }, {
      minArea: 500,
      maxArea: 1000,
      text: '500~1000m²'
    }, {
      minArea: 1000,
      maxArea: 2000,
      text: '1000~2000m²'
    }, {
      minArea: 2000,
      maxArea: 4000,
      text: '2000~4000m²'
    }, {
      minArea: 4000,
      maxArea: 0,
      text: '4000m²以上'
    }],
    areaType: {
      minArea: 0,
      maxArea: 0,
      text: '不限'
    },
    priceRangeType: [{
      minPrice: 0,
      maxPrice: 0,
      text: '不限'
    }, {
      minPrice: 0,
      maxPrice: 500,
      text: '0~500元/月'
    }, {
      minPrice: 500,
      maxPrice: 1000,
      text: '500~1000元/月'
    }, {
      minPrice: 1000,
      maxPrice: 2000,
      text: '1000~2000元/月'
    }, {
      minPrice: 2000,
      maxPrice: 4000,
      text: '2000~4000元/月'
    }, {
      minPrice: 4000,
      maxPrice: 99999,
      text: '4000元/月以上'
    }],
    showDistrictList:true,
    showAreaList:false,
    areaTypeSelected:[],
    priceType: {
      minPrice: 0,
      maxPrice: 0,
      text: '不限'
    },
    districtType: {
      id: 0,
      name: '不限'
    },
    districtTypeRange: [],
    districtTypeSelected:[],
    limit: 8,
    offset: 0,
    total: 0, // 总数
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        listData: [],
        offset: 0,
        total: 0, // 总数
      }
    }
  },
  effects: {
    // 获取列表数据
    * loadList({payload = {}}, {select, put, call}) {
      const {
        limit, listData,
        houseType, areaType, priceType, districtType, floorType
      } = yield select(({houseHome}) => houseHome)
      const {maxArea, minArea} = areaType;
      const {maxPrice, minPrice} = priceType;
      let districtId = districtType.id;
      const param = {
        limit, maxArea, minArea, maxPrice, minPrice, districtId,
        offset: listData.length, floor: floorType.value
      }

      let url = '/service/' + houseType.type + '/getPage';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'POST',
      })

      if (data && data.rows instanceof Array) {

        if (data.rows.length == 0) {
          Taro.showToast({
            title: (data || {}).msg || '没有更多数据',
            icon: 'none'
          })
        } else {
          const d = data.rows || {};
          yield put({
            type: 'updateState',
            payload: {
              listData: listData.concat(d),
              total: data.total
            }
          })
        }

      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/district/all',
        method: 'get',
      })

      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
        //添加
     * subscribe({payload = {}}, {select, call}) {
          const {
            areaTypeSelected,districtTypeSelected
          } = yield select(({houseHome}) => houseHome)

          const param ={
            "area":areaTypeSelected,
            "district":districtTypeSelected
           }
          let url = 'system/subscribe/add';

          const {data} = yield call(request, {
            url: SERVERURL + url,
            data: param,
            method: 'post',
          })
          if (data && data === "unauthc") {
            Taro.navigateTo({
              url: `/pages/login/index`
            })
            Taro.showToast({
              title: ("会话过时"),
              icon: 'none'
            })
          }
          if (data.code === '000000') {
            Taro.showToast({
              title: (data || {}).msg || '订阅成功',
              icon: 'none'
            })
          } else {
            Taro.showToast({
              title: (data || {}).msg || '订阅失败',
              icon: 'none'
            })
          }
        },
    //取消订阅
     * cancelSubscribe({payload = {}}, {select, call}) {
      Taro.showToast({
        title: '取消成功',
        icon: 'none'
      })
      // let url = '';
      // const {data} = yield call(request, {
      //   url: SERVERURL + url,
      //   data: param,
      //   method: 'post',
      // })
      // if (data && data === "unauthc") {
      //   Taro.navigateTo({
      //     url: `/pages/login/index`
      //   })
      //   Taro.showToast({
      //     title: ("会话过时"),
      //     icon: 'none'
      //   })
      // }
      // if (data.code === '000000') {
      //   Taro.showToast({
      //     title: (data || {}).msg || '取消成功',
      //     icon: 'none'
      //   })
      // } else {
      //   Taro.showToast({
      //     title: (data || {}).msg || '取消失败',
      //     icon: 'none'
      //   })
      // }
    }
  }
};
